package com.group.sliding.config;

import com.group.sliding.interceptor.AdminInterceptor;
import com.group.sliding.interceptor.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class MvcConfig implements WebMvcConfigurer {
    private final LoginInterceptor loginInterceptor;
    private final AdminInterceptor adminInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/users/login", "/users/register")
                .excludePathPatterns("/error", "/favicon.ico", "/v2/**", "/v3/**", "/swagger-resources/**", "/webjars/**", "/doc.html");
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/error", "/favicon.ico", "/v2/**", "/v3/**", "/swagger-resources/**", "/webjars/**", "/doc.html");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有的当前站点的请求地址，都支持跨域访问。
                .allowedOrigins("http://localhost:5000", "http://127.0.0.1:5000")
                .allowCredentials(true) // 是否支持跨域用户凭证
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE", "OPTIONS"}); // 当前站点支持的跨域请求类型是什么
    }
}
